package com.servitware.cardio.dokstat.store;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.servitware.cardio.dokstat.config.Configuratio;
import com.servitware.cardio.domain.transport.SolicitudTx;
import com.servitware.util.FileUtil;
import com.servitware.util.store.SerialStorer;

public class SerialStoreManager extends SerialStorer
{
	private File dir;

	public SerialStoreManager()
	{
	}

	public SerialStoreManager(File dir)
	{
		this.dir = dir;
	}

	/**
	 * Serializa objeto de Configuracion
	 * 
	 * @param cfg
	 */
	public void writeConfig(Configuratio cfg) throws FileNotFoundException, ClassNotFoundException, IOException
	{
		File file = new File(StoreFile.Config.getFileName());
		write(file, cfg);
	}

	/**
	 * Deserializa Configuration
	 * 
	 * @return
	 */
	public Configuratio readConfig() throws FileNotFoundException, ClassNotFoundException, IOException
	{
		File file = new File(StoreFile.Config.getFileName());
		
		if (!FileUtil.checkFile(file))
			return null;

		return (Configuratio) read(file);
	}

	/**
	 * Serializa listas de trabajo
	 * 
	 * @param list
	 */
	public void writeWorkingList(Map<String, List<SolicitudTx>> obj) throws FileNotFoundException, ClassNotFoundException, IOException
	{
		File file;
		if (dir == null)
			file = new File(StoreFile.ListasTrabajo.getFileName());
		else
			file = new File(dir + File.separator + StoreFile.ListasTrabajo.getFileName());

		write(file, obj);
	}

	/**
	 * Deserializa lista de descarga
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, List<SolicitudTx>> readWorkingList() throws FileNotFoundException, ClassNotFoundException, IOException
	{
		File file;
		
		if( dir == null )
			file = new File( StoreFile.ListasTrabajo.getFileName() );
		else 
			file = new File( dir + File.separator + StoreFile.ListasTrabajo.getFileName() );

		if (!FileUtil.checkFile(file))
			return null;

		return (Map<String, List<SolicitudTx>>) read(file);
	}

	/**
	 * Serializa Lista de descarga
	 * 
	 * @param list
	 */
	public void writeDownloadList(List<SolicitudTx> list) throws FileNotFoundException, ClassNotFoundException, IOException
	{
		File file;
		
		if( dir == null)
			file = new File( StoreFile.ListaDescarga.getFileName() );
		else
			file = new File( dir + File.separator + StoreFile.ListaDescarga.getFileName() );
		
		write(file, list);
	}

	/**
	 * Deserializa lista de descarga
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SolicitudTx> readDownloadList() throws FileNotFoundException, ClassNotFoundException, IOException
	{
		File file;
		if( dir == null )
			file = new File( StoreFile.ListaDescarga.getFileName() );
		else
			file = new File( dir + File.separator + StoreFile.ListaDescarga.getFileName() );

		if (!FileUtil.checkFile(file))
			return null;

		return (List<SolicitudTx>) read(file);

	}


}
